package cn.jiyun.demo;

import cn.jiyun.dongman.User;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ThymeleafDemo {

    public static void main(String[] args) {
        /*基于Thymeleaf生成静态页面*/

/*1、准备数据*/
        Context context = new Context();
     /*demo-1*/
        HashMap<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("name","赵成鑫");
     /*demo-2*/
        User user = new User(1,"刘备","123");
        dataMap.put("user",user);
     /*demo-3*/
        ArrayList<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        dataMap.put("list",list);
     /*demo-4*/
        dataMap.put("age",19);

        context.setVariables(dataMap);

/*2、准备Thymeleaf模板引擎对象*/

     /*模板解析器*/
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
     /*设置模板模式html*/
        templateResolver.setTemplateMode(TemplateMode.HTML);
     /*设置模板后缀*/
        templateResolver.setSuffix(".html");

     /*关联模板引擎和模板引擎解析器*/
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);

     /*创建文件输出流*/
        File file = new File("D:/test_out.html");
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(file,"utf-8");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
/*3、基于模板引擎生成页面*/

        templateEngine.process("test",context,writer);

    }
}
